package Scraping;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 * @author Fede
 */
public class BaseDatos5 {
    
     SqlConnector5 connector;

    public BaseDatos5(int valor) {//1-Scraping: pisa BD, 2-Archivador: no pisa BD
        
         try{
                connector = new SqlConnector5();
                connector.connect();
               
            }
            catch(Exception e)
            {
                System.out.println("Error en la Coneccion a la BD");
                System.out.println(e.toString());
            }
        if(valor==1)
        {
             reiniciarBD();
        }
    }
    
    private void reiniciarBD()
    {
        try {
            this.connector.executeStatement("delete from links;");
            this.connector.executeStatement("alter TABLE links AUTO_INCREMENT=1;");
        } catch (SQLException ex) {
            System.out.println("ERROR en reiniciar la BD");
            System.out.println(ex.toString());
        }
        
    }
    
    public boolean contains(String url) {
        try {
            
            ResultSet result = connector.executeStatementQuery("select * from links where link like '"+url+"';");
            
            result.last();
            int a= result.getRow();
            
            if(result.getRow()==0){
                return false;
            }
            else{
                return true;
            }
        } catch (SQLException ex) {
            System.out.println("Error en la lectura de la tabla de links");
        }
        return false;
    }

    public boolean add(String url) {
        try {
            this.connector.executeStatement("insert into links (link) values ('"+url+"');");
            return true;
        } catch (SQLException ex) {
            System.out.println("Error al insertar dato");
            System.out.println(ex.toString());
            return false;
        }
        
    }
    
    public String returnURL(int i)
    {
        try {
            ResultSet result = connector.executeStatementQuery("select link from links where idlinks="+i+";");
            result.last();
            if(result.getRow()==1)
                return result.getString("link");
            
            return "";
            
        } catch (SQLException ex) {
            System.out.println("ERROR al leer la URL en la base de datos");
            System.out.println(ex.toString());
            return "";
        }
        
    }
    
    public int mayorID()
    {
        try {
            ResultSet result = this.connector.executeStatementQuery("select MAX(idlinks)from links;");
            
            result.last();
            int t = result.getRow();
            if(result.getRow()==1)
            {
                int i =result.getInt("MAX(idlinks)");
                return result.getInt(1);
            }
            return -1;
            
        } catch (SQLException ex) {
            System.out.println("ERROR al leer el maximo idlink");
            System.out.println(ex.toString());
            return -1;
        }
    }
    
    
}
